package org.six11.olive3.picturephone.server.handler;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.ServletException;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.net.OliveStreamHandler;
import org.six11.olive3.picturephone.server.PPMessageNames;
import org.six11.olive3.server.DatabaseThing;
import org.six11.olive3.server.SqlStrings;
import org.six11.util2.Debug;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class RateRequestMessageHandler {

  private static Random entropy = new Random(System.currentTimeMillis());
  private DatabaseThing db;

  public RateRequestMessageHandler(DatabaseThing db) {
    this.db = db;
  }

  public boolean handle(String todoID, String sketchID, String uid, PrintWriter out) {
    boolean ret = false;
    try {
        List<String> data = db.getRowAsList(SqlStrings.FIND_JUDGEMENT, sketchID);
        if (data.size() > 0) {
          String s1_id = data.get(0);
          String s1_data = data.get(1);
          String s2_id = data.get(2);
          String s2_data = data.get(3);
          if (s1_data == null || s1_data.length() == 0) {
            s1_data = SketchRequestMessageHandler.FAKE_PEN_MESSAGE;
          }
          if (s2_data == null || s2_data.length() == 0) {
            s2_data = SketchRequestMessageHandler.FAKE_PEN_MESSAGE;
          }
          Message rateA = OliveStreamHandler.parseMessagesFromString(s1_data).get(0);
          rateA.setName(PPMessageNames.MSG_RATE_RES);
          rateA.addParam(PPMessageNames.PARAM_SKETCH_ID, s1_id);
          rateA.addParam(PPMessageNames.PARAM_SURFACE_TARGET, "a");

          Message rateB = OliveStreamHandler.parseMessagesFromString(s2_data).get(0);
          rateB.setName(PPMessageNames.MSG_RATE_RES);
          rateB.addParam(PPMessageNames.PARAM_SKETCH_ID, s2_id);
          rateB.addParam(PPMessageNames.PARAM_SURFACE_TARGET, "b");

          out.write(rateA.toOliveString());
          out.write(rateB.toOliveString());
          bug("Serviced a rate request. handed over sketches " + s1_id + " and " + s2_id
              + " to user " + uid);
          db.update(SqlStrings.DELETE_TODO_RATE, todoID);
          ret = true;
      } else {
        bug("Could not find judgement for " + sketchID);
//        Message error = new Message(PPMessageNames.MSG_ERROR);
//        error.addParam(PPMessageNames.PARAM_TEXT, //
//            "Could not find any sketches for user " + uid + " to rate. Sorry. " + //
//                "This means you rated everything that you can based on " + //
//                "the PicturePhone database. More likely than not this is an error and you " + //
//                "should email Gabe (johnsogg@cmu.edu). But if you play all the time it might " + //
//                "be legitimate. I dunno.");
//        out.write(error.toOliveString());
//        bug("Did not find anything. Sent an error message.");
      }
    } catch (ServletException ex) {
      ex.printStackTrace();
    }
    return ret;
  }
  
//  public void handleNowOldAndDumb(Message m, PrintWriter out) {
//    String uid = m.getParamValue(PPMessageNames.PARAM_USER_ID);
//    try {
//      Map<String, String> todoValues = db.getRow(SqlStrings.FIND_TODO_JUDGEMENT, uid);
//      if (!todoValues.isEmpty()) {
//        String sketchID = todoValues.get("sketch_id");
//        List<String> data = db.getRowAsList(SqlStrings.FIND_JUDGEMENT, sketchID);
//        if (data.size() > 0) {
//          String s1_id = data.get(0);
//          String s1_data = data.get(1);
//          String s2_id = data.get(2);
//          String s2_data = data.get(3);
//          if (s1_data == null || s1_data.length() == 0) {
//            s1_data = SketchRequestMessageHandler.FAKE_PEN_MESSAGE;
//          }
//          if (s2_data == null || s2_data.length() == 0) {
//            s2_data = SketchRequestMessageHandler.FAKE_PEN_MESSAGE;
//          }
//          Message rateA = OliveStreamHandler.parseMessagesFromString(s1_data).get(0);
//          rateA.setName(PPMessageNames.MSG_RATE_RES);
//          rateA.addParam(PPMessageNames.PARAM_SKETCH_ID, s1_id);
//          rateA.addParam(PPMessageNames.PARAM_SURFACE_TARGET, "a");
//
//          Message rateB = OliveStreamHandler.parseMessagesFromString(s2_data).get(0);
//          rateB.setName(PPMessageNames.MSG_RATE_RES);
//          rateB.addParam(PPMessageNames.PARAM_SKETCH_ID, s2_id);
//          rateB.addParam(PPMessageNames.PARAM_SURFACE_TARGET, "b");
//
//          out.write(rateA.toOliveString());
//          out.write(rateB.toOliveString());
//          bug("Serviced a rate request. handed over sketches " + s1_id + " and " + s2_id
//              + " to user " + uid);
//          db.update(SqlStrings.DELETE_TODO_RATE, todoValues.get("id"));
//        }
//      } else {
//        Message error = new Message(PPMessageNames.MSG_ERROR);
//        error.addParam(PPMessageNames.PARAM_TEXT, //
//            "Could not find any sketches for user " + uid + " to rate. Sorry. " + //
//                "This means you rated everything that you can based on " + //
//                "the PicturePhone database. More likely than not this is an error and you " + //
//                "should email Gabe (johnsogg@cmu.edu). But if you play all the time it might " + //
//                "be legitimate. I dunno.");
//        out.write(error.toOliveString());
//        bug("Did not find anything. Sent an error message.");
//      }
//    } catch (ServletException ex) {
//      ex.printStackTrace();
//    }
//  }
  
  private void bug(String what) {
    Debug.out("RateRequestMessageHandler", what);
  }

}
